package jframe.xfire.factory;

import java.util.HashMap;
import java.util.Map;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

/**
 * @描述:<p>Service服务端xml解析工厂 </p>
 *
 * @作者: 叶平平(yepp)
 *
 * @时间: 2011-12-30 下午10:18:04
 */
public class ServiceFactory {
	private static Map<ServiceMethod, IXmlHelper> map = new HashMap<ServiceMethod, IXmlHelper>();
	
	public static IXmlHelper getHelper(ServiceMethod method) {
		if(!map.containsKey(method)){
			map.put(method, createHelper(method));
		}
		return map.get(method);
	}
	//工厂工具类
	private static IXmlHelper createHelper(ServiceMethod method){
		IXmlHelper helper = null;
		try {
			switch (method) {
				case query://查询
				//	helper = buildHelper(DriversRequest.class,DriversResponseRoot.class);
					break;
				case add://添加
				//	helper = buildHelper();
					break;
				case delete://删除
				//	helper = buildHelper();
					break;
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return helper;
	}
	
	@SuppressWarnings({ "unchecked", "unused" })
	private static IXmlHelper buildHelper(Class request,Class response){
		IXmlHelper helper = null;
		try {
			JAXBContext context = JAXBContext.newInstance(response);
			Unmarshaller unmarshaller = context.createUnmarshaller();
			context = JAXBContext.newInstance(request);
			Marshaller marshaller = context.createMarshaller();
			helper = new XmlHelper(marshaller,unmarshaller);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return helper;
	}
}
